window.userName = $('._buryDot-userEmail')[0] ? $('._buryDot-userEmail').text() : '游客';
var das = {
	data: {
		api_id: null,
		service_id: null,
		platform: null,
		amount: null,
		platform: null

	},
	// 格式 {url: xxx,type: post,data: {}}
	$formSubmit: function(data) {
		var formInnHTML = ['<form style="display:none" action="' + (data.url || '#') + '" method="' + (data.type || 'post') + '">']
		for(var k in data.data) {
			formInnHTML.push('<input type="text" name="' + k + '" value="' + data.data[k] + '" />');
		}
		formInnHTML.push('<input type="submit" /></form>');
		var submitFrom = $(formInnHTML.join(''))[0];
		$('body').append(submitFrom);
		submitFrom.submit();
	},
	init: function() {
		// 初始化获取数据
		var datasDom = $('._order_info_data');
		this.data.api_id = datasDom.attr('data-api-id');
		this.data.package_name = datasDom.attr('data-package-name');
		this.data.amount = datasDom.attr('data-pay-price');
		this.data.service_id = datasDom.attr('data-service-id');
		this.data.platform = 1;

		$('.zfb,.wx').click(function() {
			$('.zfb,.wx').removeClass('active');
			$(this).addClass('active');
		})
		var _this = this;
		$('._pay_info_pay').click(function() {
			//1 = 支付宝； 2 = 微信支付
			_this.data.platform = ($('.zfb.active')[0] && 1) || ($('.wx.active')[0] && 2);
			for(var k in _this.data) {
				if(!_this.data[k]) {
					toastr.error('该页面已过期',null,opts);
					return;
				}
			}
			_this.$formSubmit({
				url: '/pay/pay',/// /pay/repay
				type: 'post',
				data: _this.data
			});
		});
		//重新支付
		$('._pay_keeppay_rePay').click(function() {
			//todo 埋点
			zhuge.track('开始支付',{
				'用户':userName,
				'支付方式':($('.zfb.active')[0] && '支付宝') || ($('.wx.active')[0] && '微信'),
				'金额':$('._paMoney').text()
			})
			//1 = 支付宝； 2 = 微信支付
			var repay_data = {};
			repay_data.platform = ($('.zfb.active')[0] && 1) || ($('.wx.active')[0] && 2);
			repay_data.order_id = $('._order_info_data').attr('data-order-id');

			for(var k in repay_data) {
				if(!repay_data[k]) {
					toastr.error('该页面已过期',null,opts);
					return;
				}
			}

			_this.$formSubmit({
				url: '/pay/repay',
				type: 'post',
				data: repay_data
			});
		});
	}
}
das.init()

/**
 * 格式化时间
 * 如果没有分隔符 则返回 YYYYMMDD , 如果输入分隔符如'-'则返回YYYY-MM-DD
 * @param data 可以被new Date解析的时间数据
 * @param _ 分隔符
 * @returns {string}
 */
function getDate(int) {
	return new Date(+new Date() - int * 86400000);
	
	var month = time.getMonth() + 1;
	var _data = time.getDate();
	if(month < 10) month = "0" + month;
	if(_data < 10) _data = "0" + _data;
	var year = [time.getFullYear(), month, _data];
	return year.join(_ || "");
}